#include "TwoTapsSeparateLeftRight.h"
#include <iostream>

#define NAME L"TwoTapsSeparateLeftRight"

TwoTapsSeparateLeftRight::TwoTapsSeparateLeftRight()
{
	_name = wstring(NAME);
	_err = wstring(L"NONE");
}
TwoTapsSeparateLeftRight::~TwoTapsSeparateLeftRight()
{
}
bool TwoTapsSeparateLeftRight::Inited()
{
	return true;
}

void TwoTapsSeparateLeftRight::Close()
{
	delete this;
}

int TwoTapsSeparateLeftRight::Decode(unsigned char *dest, int dest_buf_len, unsigned char *source, int color_depth, int column, int line, DecodeRequestStruct* args/* = 0*/)
{
	if (dest_buf_len < (column * line * ((color_depth + 7) / 8)))
		return 0;
	if (color_depth == 8) {
		for (int i = 0; i < line; i++) {
			for (int j = 0; j < column; j++) {
				dest[i * column + j / 2 + (column / 2) * (j % 2)] = source[i * column + j];
			}
		}
	} else {
		unsigned short *_dest = (unsigned short *)dest;
		unsigned short *_source = (unsigned short *)source;
		for (int i = 0; i < line; i++) {
			for (int j = 0; j < column; j++) {
				_dest[i * column + j / 2 + (column / 2)  * (j % 2)] = _source[i * column + j];
			}
		}
	}
	return 0;
}

const wchar_t* TwoTapsSeparateLeftRight::Name()
{
	return _name.c_str();
}
const wchar_t* TwoTapsSeparateLeftRight::ErrorInfo()
{
	return _err.c_str();
}

void* GetDecoderPlugin(int version)
{
	if (version == DECODER_PLUGIN_INTERFACE_VERSION){
		return new TwoTapsSeparateLeftRight();
	} else{
		return NULL;
	}
}